#-------------------------------------------------------------------------------
#
# Electoral predictors - pc expenditures results
#
# Author: Sina Chen
#
#-------------------------------------------------------------------------------


# Libraries ---------------------------------------------------------------

{
  library(rv)
  library(gtools)
  library(ggplot2)
  library(rstan)
  library(dplyr)
  library(scales)
  library(shinystan)
  library(RColorBrewer)
  library(ggridges)
  library(cowplot)
}

setnsims(10000)


# Data --------------------------------------------------------------------

# polls
polls <- readRDS("data/us_senate_polls1990_2022_final.RDS")

# simulation results
resStan <- readRDS('code/fit_stan/resStan_us_senate_context1990_2022_exp.RDS')


# Preparation -------------------------------------------------------------

polls <- polls %>%
  filter(dte < 101 & !is.na(exp_pc_rep) & !is.na(exp_pc_dem)) %>% 
  group_by(state, cycle) %>% 
  mutate(n_poll = n()) %>% 
  ungroup() %>% 
  filter(n_poll >= 5) %>% 
  mutate(state_year = paste0(state, cycle),
         cycle = as.integer(cycle),
         state_year_int = as.integer(as.factor(state_year)),
         t_sc = as.numeric(dte)/max(as.numeric(dte)))

# election-level data 
election_data <- polls %>%
  group_by(state_year, state_year_int, cycle, state, vote2_rep, exp_pc_rep, 
           exp_pc_dem) %>%
  summarise() %>%
  ungroup()

# convert simulations to random variable (rv) obj.
postrv <- as.rv(resStan)



# Compute estimates -------------------------------------------------------

#### Election level excess variance ####

#  excess sd
ex_sd_r <- sqrt(postrv$mu_phi2 + postrv$phi2_sc*postrv$sig_phi2 + 
                  postrv$gamma*log(election_data$exp_pc_dem + election_data$exp_pc_rep))

summary_ex_sd_r <- summary(ex_sd_r)
summary_ex_sd_r$state_year_int <- seq(1, nrow(election_data))
summary_ex_sd_r <- merge(summary_ex_sd_r, election_data, by = "state_year_int")

# expected excess sd
exp_ex_sd_r <- sqrt(postrv$mu_phi2 +
                      postrv$gamma*log(election_data$exp_pc_dem + election_data$exp_pc_rep))

summary_exp_ex_sd_r <- summary(exp_ex_sd_r)
summary_exp_ex_sd_r$state_year_int <- seq(1, nrow(election_data))
summary_exp_ex_sd_r <- merge(summary_exp_ex_sd_r, election_data, by = "state_year_int")

# save
saveRDS(summary_ex_sd_r, "code/results_vis/us_senate_ex_sd_exp.RDS")
saveRDS(summary_exp_ex_sd_r, "code/results_vis/us_senate_exp_ex_sd_exp.RDS")









